#include "graphmovewindow.h"

#include <QPaintEvent>
#include <QPainter>
#include <QDebug>
#include <QIcon>
#include <QTimer>

GraphMoveWindow::GraphMoveWindow(QWidget *parent)
    : QWidget(parent)
    , m_currentPos(QPoint(100, 100))
    , m_goalPos(m_currentPos)
    , m_xDir(0)
    , m_yDir(0)
    , m_speed(2.0)
{
    m_cursorImg = QIcon(":/icons/cursor.svg").pixmap(QSize(50, 50)).toImage();

    QTimer *dateUpdateTimer = new QTimer(this);
    dateUpdateTimer->setInterval(10);

    QTimer *paintUpdateTimer = new QTimer(this);
    paintUpdateTimer->setInterval(20); // 帧率大约为1000/20

    connect(dateUpdateTimer, &QTimer::timeout, this, &GraphMoveWindow::updateDate);
    connect(paintUpdateTimer, &QTimer::timeout, this, &GraphMoveWindow::updateGraphicPaint);

    dateUpdateTimer->start();
    paintUpdateTimer->start();
}

GraphMoveWindow::~GraphMoveWindow()
{
}

void GraphMoveWindow::mousePressEvent(QMouseEvent *event)
{
    QWidget::mousePressEvent(event);

    if (Qt::MouseButton::LeftButton == event->button()) {
        m_goalPos = event->pos();
        qInfo() << "m_goalPos" << m_goalPos;
    } else if (Qt::MouseButton::RightButton == event->button()) {
        m_goalPos = event->pos();
        m_isShowRightMouseBox = true;
        qInfo() << "m_goalPos" << m_goalPos;
    }
}

void GraphMoveWindow::mouseReleaseEvent(QMouseEvent *event)
{
    QWidget::mouseReleaseEvent(event);

    if (Qt::MouseButton::RightButton == event->button()) {
        m_isShowRightMouseBox = false;
    }
}

void GraphMoveWindow::wheelEvent(QWheelEvent *event)
{
    QWidget::wheelEvent(event);

    m_goalPos += QPoint(0, -event->delta() / 10);

    if (m_goalPos.y() > geometry().bottom() - 50) {
        m_goalPos.ry() = geometry().bottom() - 50;
    } else if (m_goalPos.y() < 10) {
        m_goalPos.ry() = 10;
    }

    updateGraphicPaint();
}

void GraphMoveWindow::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);

    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); //设置渲染特性：反锯齿

    painter.drawImage(m_currentPos - QPoint(25, 25), m_cursorImg);
    painter.drawArc(m_goalPos.x() - 5, m_goalPos.y() - 5, 10, 10, 0, 360 * 16);
    painter.drawText(m_goalPos.x() - 30, m_goalPos.y() + 30, "目标位置");

    // 绘制右键方格
    if (m_isShowRightMouseBox) {
        painter.drawRect(m_goalPos.x() - 7, m_goalPos.y() - 8, 15, 15);
    }

    painter.end();
}

const int HighSpeed = 5;
const int NormalSpeed = 1;

void GraphMoveWindow::updateDate()
{
    //    qInfo() << Q_FUNC_INFO << "距离上次更新间隔ms" << -QTime::currentTime().msecsTo(m_time);
    if (m_goalPos != m_currentPos) {
        int xDistance = m_goalPos.x() - m_currentPos.x();
        int xSpeed = NormalSpeed;
        if (5 < xDistance) {
            m_xDir = 1;
            xSpeed = HighSpeed;
        } else if (0 < xDistance) {
            m_xDir = 1;
        } else if (-5 > xDistance) {
            m_xDir = -1;
            xSpeed = HighSpeed;
        } else {
            m_xDir = -1;
        }

        int yDistance = m_goalPos.y() - m_currentPos.y();
        int ySpeed = NormalSpeed;
        if (5 < yDistance) {
            m_yDir = 1;
            ySpeed = HighSpeed;
        } else if (0 < yDistance) {
            m_yDir = 1;
        } else if (0 == yDistance) {
            m_yDir = 0;
        } else if (-5 > yDistance) {
            m_yDir = -1;
            ySpeed = HighSpeed;
        } else {
            m_yDir = -1;
        }

        m_currentPos += QPoint(m_xDir * xSpeed, m_yDir * ySpeed);
        //        qInfo() << "m_currentPos" << m_currentPos;
        //        qInfo() << "m_xDir" << m_xDir << "m_yDir" << m_yDir;
    }

    m_time = QTime::currentTime();
}

void GraphMoveWindow::updateGraphicPaint()
{
    update();
}
